package cn.asiawu.springboot.service;

import cn.asiawu.springboot.common.Constants;
import cn.asiawu.springboot.controller.dto.UserDto;
import cn.asiawu.springboot.entity.User;
import cn.asiawu.springboot.exception.ServiceException;
import cn.asiawu.springboot.mapper.UserMapper;
import cn.asiawu.springboot.utils.TokenUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Service
public class UserService extends ServiceImpl<UserMapper,User> {
    @Autowired
    private UserMapper userMapper;
    public List<Integer> getDataInAWeek(){
        //调用usermapper得到一周内每天注册人数包装成list返回
        Integer one=userMapper.getDataInOneDay();
        Integer two=userMapper.getDataInTwoDay();
        Integer three=userMapper.getDataInThreeDay();
        Integer four=userMapper.getDataInFourDay();
        Integer five=userMapper.getDataInFiveDay();
        Integer six=userMapper.getDataInSixDay();
        Integer seven=userMapper.getDataInSevenDay();
        List<Integer> list=new ArrayList<>(Arrays.asList(one,two-one,three-two,four-three,five-four,six-five,seven-six));
        Collections.reverse(list);
        return list;
    }
    public boolean saveUser(User user) {
        //有bug，暂时不开放
//        if (judgeUsername(user.getUsername()))
//            throw new ServiceException(Constants.CODE_600,"用户名已存在");
//        else
        return saveOrUpdate(user);
    }

    //登录
    public UserDto login(UserDto userDto) {
        //根据用户名和密码匹配数据
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",userDto.getUsername());
        queryWrapper.eq("password",userDto.getPassword());
        List<User> list=list(queryWrapper);
        if (list.size()==1) {
            User user=list.get(0);
            //设置token
            String token = TokenUtils.generateToken(user.getId().toString(), user.getPassword());
            return new UserDto(user.getUsername(),user.getPassword(),user.getNickname(),user.getAvatar(),token,user.getRole());
        }
        else if (list.size()==0){
            throw new ServiceException(Constants.CODE_600,"用户名或密码错误");
        }
        //脏数据情况，但只能是用户名和密码相同时，有bug，待修改
        else {
            throw new ServiceException(Constants.CODE_600,"多个用户");
        }
    }

    //注册
    public UserDto register(UserDto userDto) {
        if (judgeUsername(userDto.getUsername()))
            throw new ServiceException(Constants.CODE_600,"用户名已存在");
        else {
            User user=new User(null,userDto.getUsername(),userDto.getPassword(),null,null,null,null,"https://pic.616pic.com/ys_bnew_img/00/03/79/eFDppg6fgB.jpg",null,null);
            save(user);
            return userDto;
        }
    }

    private boolean judgeUsername(String username) {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",username);
        List<User> list=list(queryWrapper);
        if (list.size()>=1)
            return true;
        else
            return false;
    }
//    @Autowired
//    private UserMapper userMapper;

//    public int save(User user)
//    {
//        if (user.getId()==null) {
//            return userMapper.insert(user);
//        }
//        else {
//            return userMapper.update(user);
//        }
//    }
}
